﻿    function imgAeraSelectAndPrint(selector) {
        var imgAreaSelect = $(selector).imgAreaSelect({
            handles: true,
            instance: true,
            keys: true
        });
        $(selector).bind('contextmenu', function (event) {
            var event = event || window.event;
            var imgSeletion = imgAreaSelect.getSelection();
            if (imgSeletion == null || imgSeletion.width == 0 || imgSeletion.height == 0) {
                event.preventDefault();
                var imgSrc = $(selector).attr('src');
                var imgContainerHeight = $(selector).height();
                var imgContainerWidth = $(selector).width();
                var selectedImgLeft = 0;
                var selectedImgTop = 0;
                var selectedImgHeight = $(selector).height();
                var selectedImgWidth = $(selector).width();
                showPrintMenu(event, imgSrc, imgContainerHeight, imgContainerWidth, selectedImgLeft, selectedImgTop, selectedImgHeight, selectedImgWidth,'no');
            }
        });
        $(document).bind('contextmenu', function (event) {
            var event = event || window.event;
            var imgSeletion = imgAreaSelect.getSelection();
            if (imgSeletion != null && imgSeletion.width != 0 && imgSeletion.height != 0) {
                event.preventDefault();
                var imgSrc = $(selector).attr('src');
                var imgContainerHeight = imgSeletion.height;
                var imgContainerWidth = imgSeletion.width;
                var selectedImgLeft = -imgSeletion.x1;
                var selectedImgTop = -imgSeletion.y1;
                var selectedImgHeight = $(selector).height();
                var selectedImgWidth = $(selector).width();
                showPrintMenu(event, imgSrc, imgContainerHeight, imgContainerWidth, selectedImgLeft, selectedImgTop, selectedImgHeight, selectedImgWidth, 'no');
            }
        });
        $(selector).click(function () {
            $('#A09125CF-A460-4254-B6C9-E3E2AB59A037').remove();
        });
    }

    function showPrintMenu(event, imgSrc, imgContainerHeight, imgContainerWidth, selectedImgLeft, selectedImgTop, selectedImgHeight, selectedImgWidth, isPrint) {
        $('#A09125CF-A460-4254-B6C9-E3E2AB59A037').remove();
        var btnDiv = $('<div id="A09125CF-A460-4254-B6C9-E3E2AB59A037"></div>').appendTo(window.document.body);
        $(btnDiv).attr('style', 'left: ' + event.pageX + 'px; top: ' + event.pageY + 'px;display: block; position: absolute; z-index: 999;zoom: 1;')
        var button = $('<button type="button" class="sexybutton""></button>').appendTo(btnDiv);
        $(button).click(
                        function () {
                            openWindow(imgSrc, imgContainerHeight, imgContainerWidth, selectedImgLeft, selectedImgTop, selectedImgHeight, selectedImgWidth, isPrint);
                        });
        $('<span><span><span class="upload">打印</span></span></span>').appendTo(button);
    }

    function openWindow(imgSrc, imgContainerHeight, imgContainerWidth, selectedImgLeft, selectedImgTop, selectedImgHeight, selectedImgWidth, isPrint) {
        var printWindow = window.open('ImageCutAndPrint.aspx?imgSrc=' + imgSrc + '&selectHeight=' + imgContainerHeight + '&selectWidth=' + imgContainerWidth + '&selectedImgLeft=' + (-selectedImgLeft) + '&selectedImgTop=' + (-selectedImgTop) + '&selectedImgWidth=' + selectedImgWidth + '&selectedImgHeight=' + selectedImgHeight+'&print='+isPrint, 'PrintImage', 'height= 500,width = 800,resizable= yes, scrollbars = yes');
    }